package cn.csc.module.shop.service.user;

import cn.csc.framework.common.exception.ServiceException;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.member.api.user.MemberUserApi;
import cn.csc.module.member.api.user.dto.MemberUserRespDTO;
import cn.csc.module.shop.controller.admin.user.vo.ShopUserCreateReqVO;
import cn.csc.module.shop.controller.admin.user.vo.ShopUserExportReqVO;
import cn.csc.module.shop.controller.admin.user.vo.ShopUserPageReqVO;
import cn.csc.module.shop.controller.admin.user.vo.ShopUserUpdateReqVO;
import cn.csc.module.shop.convert.user.ShopUserConvert;
import cn.csc.module.shop.dal.dataobject.detail.ShopDetailDO;
import cn.csc.module.shop.dal.dataobject.user.ShopUserDO;
import cn.csc.module.shop.dal.mysql.detail.ShopDetailMapper;
import cn.csc.module.shop.dal.mysql.user.ShopUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.shop.enums.ErrorCodeConstants.*;

/**
 * 商家用户 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class ShopUserServiceImpl implements ShopUserService {

    @Resource
    private ShopUserMapper userMapper;

    @Resource
    private ShopDetailMapper detailMapper;

    @Resource
    private MemberUserApi memberUserApi;

    @Override
    public BigInteger createUser(ShopUserCreateReqVO createReqVO) {
        // 插入
        ShopUserDO user = ShopUserConvert.INSTANCE.convert(createReqVO);
        userMapper.insert(user);
        // 返回
        return user.getId();
    }

    @Override
    public void updateUser(ShopUserUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserExists(updateReqVO.getId());
        // 更新
        ShopUserDO updateObj = ShopUserConvert.INSTANCE.convert(updateReqVO);
        userMapper.updateById(updateObj);
    }

    @Override
    public void deleteUser(BigInteger id) {
        // 校验存在
        validateUserExists(id);
        // 删除
        userMapper.deleteById(id);
    }

    private void validateUserExists(BigInteger id) {
        if (userMapper.selectById(id) == null) {
            throw exception(SHOP_USER_NOT_EXISTS);
        }
    }

    @Override
    public ShopUserDO getUser(BigInteger id) {
        return userMapper.selectById(id);
    }

    /**
     * 获得商家用户
     *
     * @param userId 用户Id
     * @return 商家用户
     */
    @Override
    public ShopUserDO getByUserId(BigInteger userId) {
        return userMapper.selectByUserId(userId);
    }

    /**
     * 获得商家用户list
     *
     * @return 商家用户
     */
    @Override
    public List<ShopUserDO> getByShopId(BigInteger userId) {
        ShopDetailDO shopDetailDO = detailMapper.selectDetailByUserId(userId);
        return userMapper.selectList(ShopUserDO::getShopId, shopDetailDO.getId());
    }

    /**
     * 关联商家用户
     *
     * @param userId     当前用户Id
     * @param shopUserId
     * @return 商家用户
     */
    @Override
    public Boolean relate(BigInteger userId, BigInteger shopUserId) {
        MemberUserRespDTO userRespDTO = memberUserApi.getUser(shopUserId.longValue()).getCheckedData();
        if (userRespDTO == null){
            throw new ServiceException(MEMBER_USER_NOT_EXISTS);
        }
        ShopDetailDO shopDetailDO = detailMapper.selectDetailByUserId(userId);
        ShopUserDO shopUserDO = ShopUserDO.builder().shopId(shopDetailDO.getId())
                .userId(shopUserId)
                .phoneNumber(userRespDTO.getMobile()).nickName(userRespDTO.getNickname()).isAdmin((byte) 0)
                .build();
        int insert = userMapper.insert(shopUserDO);

        return insert > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    /**
     * 关联商家用户
     *
     * @param userId     当前用户Id
     * @param shopId     店铺id
     * @return 商家用户
     */
    @Override
    public Boolean relateByShopId(BigInteger userId, BigInteger shopId) {
        MemberUserRespDTO userRespDTO = memberUserApi.getUser(userId.longValue()).getCheckedData();
        if (userRespDTO == null){
            throw new ServiceException(MEMBER_USER_NOT_EXISTS);
        }
        ShopDetailDO shopDetailDO = detailMapper.selectById(shopId);
        if (shopDetailDO == null){
            throw new ServiceException(SHOP_DETAIL_NOT_EXISTS);
        }
        ShopUserDO shopUserDO = ShopUserDO.builder().shopId(shopDetailDO.getId())
                .userId(userId)
                .phoneNumber(userRespDTO.getMobile()).nickName(userRespDTO.getNickname()).isAdmin((byte) 0)
                .build();
        int insert = userMapper.insert(shopUserDO);

        return insert > 0 ? Boolean.TRUE : Boolean.FALSE;

    }

    @Override
    public List<ShopUserDO> getUserList(Collection<BigInteger> ids) {
        return userMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopUserDO> getUserPage(ShopUserPageReqVO pageReqVO) {
        return userMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopUserDO> getUserList(ShopUserExportReqVO exportReqVO) {
        return userMapper.selectList(exportReqVO);
    }

}
